package com.hxy.controller;

import com.hxy.entity.Fruits;
import com.hxy.service.FruitsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("fruits")
public class FruitsController {

    @Resource
    FruitsService fruitsService;

    // 获取配置文件中得属性名对应得属性值
    @Value("${filepath}")
    String filepath;

    /**
     * 获取商品详细信息
     *
     * @param fid
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public Fruits findById(Integer fid) {
        return fruitsService.findById(fid);
    }


    @RequestMapping("list")
    @ResponseBody
    public List<Fruits> list() {
        return fruitsService.list();
    }

    @RequestMapping("test")
    @ResponseBody
    public void test() {
        fruitsService.m1(3);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        fruitsService.m2(list);

        fruitsService.m3();
        fruitsService.m4();
    }


    /**
     * 表单提交得文件要单独接收，不能直接赋值给对象得属性，类型不一致
     *
     * @param photo
     * @param fruits
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public int add(@RequestParam("photo") MultipartFile photo, Fruits fruits) {
        // 1.空文件:文件原名
        String originalFilename = photo.getOriginalFilename();
        if (!"".equals(originalFilename)) {
            // 2.覆盖：重命名
            // 保存得文件路径
            String filename = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + originalFilename;
            File file = new File(filepath + filename);
            try {
                // 另存为
                photo.transferTo(file);
                fruits.setPrcture(filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fruitsService.add(fruits);
    }



    @RequestMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filename = "D:/aaa/20210727s8.png";

        File file = new File(filename);

        // 输入流：读
        FileInputStream inputStream = new FileInputStream(file);

        // 获取文件类型
        String mimeType = request.getServletContext().getMimeType(filename);
        System.out.println(mimeType);

        // 浏览器要保存，不能直接打开
        response.setContentType(mimeType);
        // 响应头：保存,attachment附件，filename保存附件时得文件名
        response.setHeader("content-disposition","attachment;filename=a.png");

        // 读文件，写出去
        // 输出流
        ServletOutputStream outputStream = response.getOutputStream();

        // 复制文件，输入流读取文件并输出
        FileCopyUtils.copy(inputStream,outputStream);
    }
}
